package com.project.forlearning.utils;

import io.jsonwebtoken.*;
import java.util.Date;

public class TokenUtil {
    private static final String secretKey = "secret"; //密匙
    public static final String tokenHead = "token"; //请求头
    private static final Long expTime = 60 * 60 * 24 * 1000L; //过期时间

    public static String getToken(String id, String subject,String ip) {
        JwtBuilder builder = Jwts.builder()
                .setId(id)  //编号
                .setSubject(subject)  // 账号
                .setAudience(ip)  //地址
                .setIssuedAt(new Date()) // 当前时间
                .setExpiration(new Date(System.currentTimeMillis() + expTime)) //过期时间
                .signWith(SignatureAlgorithm.HS256,secretKey);
        String token = builder.compact();
        return token;
    }

    public static Claims getTokenBody(String token) {
        JwtParser parser = Jwts.parser();
        Claims body = parser.setSigningKey(secretKey).parseClaimsJws(token).getBody();
        return body;
    }

}
